// 广告页面

import React from 'react';
import { NavigationActions } from 'react-navigation';
import { WingBlank, WhiteSpace, Toast } from 'antd-mobile-rn';
import AdCard from '../../components/AdCard';
import PullList from '../../components/PullList';
import BaseView from '../BaseView';
import { getADList } from '../../model/ad';

export default class CardWoucher extends BaseView {
  constructor(props) {
    super(props);
    // this.dataSource = [
    //   {
    //     id: 1,
    //     endTime: '2018-1-1',
    //     images: 'https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=45e1c8e71ddfa9ece22e501752d1f754/342ac65c103853434cc02dda9f13b07eca80883a.jpg',
    //     status: '已发布'
    //   },
    //   {
    //     id: 2,
    //     endTime: '2018-1-1',
    //     images: 'https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=45e1c8e71ddfa9ece22e501752d1f754/342ac65c103853434cc02dda9f13b07eca80883a.jpg',
    //     status: '已发布'
    //   },
    //   {
    //     id: 3,
    //     endTime: '2018-1-1',
    //     images: 'https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=45e1c8e71ddfa9ece22e501752d1f754/342ac65c103853434cc02dda9f13b07eca80883a.jpg',
    //     status: '已发布'
    //   },
    //   {
    //     id: 4,
    //     endTime: '2018-1-1',
    //     images: 'https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=45e1c8e71ddfa9ece22e501752d1f754/342ac65c103853434cc02dda9f13b07eca80883a.jpg',
    //     status: '已发布'
    //   }
    // ];
    this.loadMore = this.loadMore.bind(this);
    this.state = {
      ...this.BaseState,
      nomore: false,
      pullList: []
    };
  }

  async componentWillMount() {
    const { navigation } = this.props;

    navigation.setParams({ goback: this.goback.bind(this) });
    Toast.loading('加载中');

    // const List = await getADList({
    //   terminalType: 'MOBILE'
    // });
    // console.log(List);
    // this.pullList.setData(List.dataObject);

    Toast.hide();
  }

  // header
  componentDidMount() {
    super.componentDidMount();
    this.props.navigation.setParams({
      title: '广告管理',
      backgroundColor: '#3ca4d9',
      textColor: '#fff',
      rightTxt: '新增', // 右侧按钮名称
      rightClick: this.toAdDetail.bind(this) // 点击右侧按钮事件
    });
    // super.setState({
    //   loading: true
    // });
  }

  // 下拉刷新调用方法
  onPullRelease = async (resolve) => {
    try {
      Toast.loading('加载中');

      // const List = await getADList({
      //   terminalType: 'MOBILE'
      // });
      // console.log(List);
      // this.pullList.setData(List.dataObject);

      Toast.hide();
      if (resolve) resolve();
    } catch (err) {
      if (resolve) resolve();
      Log(err);
    }
  };

  // 返回上一级
  goback() {
    const { navigation } = this.props;
    const backAction = NavigationActions.back();
    navigation.dispatch(backAction);
  }

  toAdDetail() {
    const { navigation } = this.props;

    const navigateAction = NavigationActions.navigate({
      routeName: 'AdvertisementForm',
      params: {}
    });
    navigation.dispatch(navigateAction);
  }

  // 加载更多
  async loadMore() {
    Toast.loading('加载中222');
    const List = await getADList({
      terminalType: 'MOBILE',
    });
    console.log(List);
    this.pullList.setData(List.dataObject);
    Toast.hide();
  }

  // 渲染列
  renderRow({ item /* , index, separators */ }) {
    return (
      <WingBlank>
        <WhiteSpace />
        <AdCard
          endTime={item.endTime}
          images={item.publicityPcture}
          status={item.status}
          key={item.id}
        />
      </WingBlank>
    );
  }

  render() {
    const { nomore } = this.state;

    return (
      /**
       * @param {boolean} nomore - 是否还有更多数据， ture：没有更多了，false：还有
       * @param {function} initData - 初始数据定义
       * @param {function} onPullRelease - 下拉刷新调用方法
       * @param {function} renderRow - 渲染列
       * @param {function} onEndReached - 加载更多
       */
      <PullList
        nomore={nomore}
        initData={(list) => {
          this.pullList = list;
        }}
        onPullRelease={this.onPullRelease}
        renderRow={this.renderRow}
        onEndReached={this.loadMore}
      />
    );
  }
}
